package com.guli.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 库存rabbitmq
 */
@Configuration
public class MyRabbitConfig {

    /**
     * rabbit 消息json化
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    //队列创建不成功，这里监听一下就可以了
//    @RabbitListener(queues = "stock.release.stock.queue")
//    public void handle(Message message){
//
//    }

    /**
     * 库存交换机
     *
     * @return
     */
    @Bean
    public Exchange stockEventExchange() {
        /**
         * TopicExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
         *  name：交换机名称
         *  durable:是否持久化
         *  autoDelete:是否自动删除
         *  arguments:参数
         */
        TopicExchange exchange = new TopicExchange("stock-event-exchange", true, false);
        return exchange;
    }


    /**
     * c端队列，死信队列超时后的接收队列（与c端交互）
     *
     * @return
     */
    @Bean
    public Queue stockReleaseStockQueue() {
        Queue queue = new Queue("stock.release.stock.queue", true, false, false);
        return queue;
    }

    /**
     * 死信队列（没有订阅方，超时自动放入其他队列，方便使用（延迟队列实现））
     * 延迟队列
     *
     * @return
     */
    @Bean
    public Queue stockDelayQueue() {
        /**
         Queue(
         String name,  队列名字
         boolean durable,  是否持久化
         boolean exclusive,  是否排他
         boolean autoDelete, 是否自动删除
         Map<String, Object> arguments 属性
         )
         */
        Map<String, Object> arguments = new HashMap<>();
        //死信交换机
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        //死信路由键
        arguments.put("x-dead-letter-routing-key", "stock.release");
        arguments.put("x-message-ttl", 120000); // 消息过期时间 50分钟

        Queue queue = new Queue("stock.delay.queue", true, false, false, arguments);
        return queue;
    }

    /**
     * 交换机绑定队列
     * @return
     */
    @Bean
    public Binding stockReleaseBinding() {
        /**
         * Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, @Nullable Map<String, Object> arguments)
         * destination：目的地
         * destinationType：目的地类型
         * exchange：交换机
         * routingKey：路由键
         * arguments：指定参数
         *
         * 将指定的交换机和目的地进行绑定，指定目的地类型
         *
         */
        Binding binding = new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#", null);
        return binding;
    }

    /**
     * 死信队列和交换机绑定
     *
     * @return
     */
    @Bean
    public Binding stockDelayBinding() {
        /**
         * Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, @Nullable Map<String, Object> arguments)
         * destination：目的地
         * destinationType：目的地类型
         * exchange：交换机
         * routingKey：路由键
         * arguments：指定参数
         *
         * 将指定的交换机和目的地进行绑定，指定目的地类型
         *
         */
        Binding binding = new Binding(
                "stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked", null);
        return binding;
    }

}
